package com.rerun.phone.memberuser;

import com.baomidou.mybatisplus.plugins.Page;
import com.rerun.common.anno.ResponseExceptionReturn;
import com.rerun.common.bean.Rest;
import com.rerun.common.controller.SuperController;
import com.rerun.entity.Job;
import com.rerun.entity.WatchedJob;
import com.rerun.service.IJobService;
import com.rerun.service.IWatchedJobService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * The type Job controller.
 *
 * @author dax.
 * @version v1.0
 * @since 2017 /9/29 20:35
 */
@Api(value = "求职端-职位相关", description = "求职端-职位相关")
@RestController
@RequestMapping("/memberApi/job")
public class JobController extends SuperController {
    @Resource
    private IJobService iJobService;
    @Resource
    private IWatchedJobService iWatchedJobService;


    /**
     * 根据用户求职意向获取分页列表.
     *
     * @param memberId   the member id
     * @param pageNumber the page number
     * @return the rest
     */
    @ApiOperation(value = "根据求职意向获取分页列表", notes = "根据求职意向获取分页列表", httpMethod = "GET")
    @RequestMapping(value = "/list/memberId/{memberId}/{pageNumber}", method = RequestMethod.GET)
    @ResponseExceptionReturn
    public Rest findJobPageByIntention(@ApiParam(required = true, name = "memberId", value = "用户id") @PathVariable("memberId") String memberId, @ApiParam(required = true, name = "pageNumber", value = "页码") @PathVariable("pageNumber") Integer pageNumber) {
        Page<Job> jobPage = getPage(pageNumber, 15);
        return Rest.okData(iJobService.findPageByIntention(jobPage, memberId));
    }

    /**
     * 根据职位发布地区获取分页列表.
     *
     * @param workDistrict the work district
     * @param pageNumber   the page number
     * @param pageSize     the page size
     * @param keyWord      the key word
     * @param workCity     the work city
     * @param edu          the edu
     * @param salaryRange  the salary range
     * @param workYears    the work years
     * @return the rest
     */
    @ApiOperation(value = "根据职位发布地区获取分页列表", notes = "根据职位发布地区获取分页列表", httpMethod = "GET")
    @RequestMapping(value = "/list/{workCity}/{pageNumber}", method = RequestMethod.GET)
    @ResponseExceptionReturn
    public Rest findJobPageByLocation(String workDistrict, @ApiParam(required = true, name = "pageNumber", value = "页码") @PathVariable("pageNumber") Integer pageNumber, @RequestParam(defaultValue = "15") Integer pageSize, @ApiParam(name = "keyWord", value = "职位名称模糊查询关键字") @RequestParam(defaultValue = "") String keyWord, @PathVariable("workCity") String workCity, String edu, String salaryRange, String workYears) {
        Page<Job> jobPage = getPage(pageNumber, pageSize);
        return Rest.okData(iJobService.findPageByWorkDistrict(jobPage, workCity, workDistrict, keyWord, edu, salaryRange, workYears));
    }

    /**
     * 获取最新10条工作信息.
     *
     * @return the rest
     */
    @ApiOperation(value = "获取最新10条工作信息", notes = "获取最新10条工作信息", httpMethod = "GET")
    @RequestMapping(value = "/list/newJobs", method = RequestMethod.GET)
    @ResponseExceptionReturn
    public Rest findNewJobs() {
        Page<Job> jobPage = getPage(1, 10);
        return Rest.okData(iJobService.findNewJobs(jobPage));
    }

    /**
     * 我查看过的职位取最近看过的5条.
     *
     * @param memberId the member id
     * @return the rest
     */
    @ApiOperation(value = "我查看过的职位取最近看过的不重复的5条", notes = "我查看过的职位取最近看过的不重复的5条", httpMethod = "GET")
    @RequestMapping(value = "/list/watchedJobs/{memberId}", method = RequestMethod.GET)
    @ResponseExceptionReturn
    public Rest findWatchedJobs(@PathVariable("memberId") String memberId) {
        List<String> idList = iWatchedJobService.selectJobIds(memberId);

        return Rest.okData(iJobService.selectBatchIds(idList));
    }

    /**
     * Add watched job rest.
     *
     * @param memberId the member id
     * @param jobId    the job id
     * @return the rest
     */
    @ApiOperation(value = "查看职位记录", notes = "求职者 查看职位详情时 作为查看历史记录", httpMethod = "POST")
    @RequestMapping(value = "/add/watchedJob/{memberId}/{jobId}", method = RequestMethod.POST)
    @ResponseExceptionReturn
    public Rest addWatchedJob(@PathVariable("memberId") String memberId, @PathVariable("jobId") String jobId) {

        WatchedJob watchedJob = iWatchedJobService.selectByMemberIdAndJobId(memberId, jobId);

        if (Objects.isNull(watchedJob)) {
            watchedJob = new WatchedJob();
            watchedJob.setMemberId(memberId);
            watchedJob.setJobId(jobId);
            watchedJob.setCreateTime(new Date());

            iWatchedJobService.insert(watchedJob);
        } else {
            watchedJob.setCreateTime(new Date());

            iWatchedJobService.updateById(watchedJob);
        }
        return Rest.ok();
    }

    /**
     * 通过id获取职位详情.
     *
     * @param id the id
     * @return the rest
     */
    @ApiOperation(value = "职位详情", notes = "通过jobId获取职位详情", httpMethod = "GET")
    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    @ResponseExceptionReturn
    public Rest findJobById(@PathVariable("id") String id) {
        return Rest.okData(iJobService.getJobDetailById(id));
    }
}
